/*  J
**  Date 2007-05-29 21:41:40 
**  File japi.c  v 1.0
**  Author Mucker
**  See Copyright Notice in J.h
**  
*/

#include "J.h"

#include "japi.h"
#include "io.h"
#include "lex.h"

#include "debug.h"

#include "table.h"
#include "chuck.h"
#include "parser.h"
#include "call.h"

struct loadF{
	FILE *fp;
	char buff[512];
};

static char *getF(struct loadF *lf, size_t *size)
{
    if(feof(lf->fp))
		return NULL;
	*size = fread(lf->buff, 1, 512, lf->fp);
	return lf->buff;
}

Jcall * J_loadF(const char *fname)
{
	FILE *fp;
	lex_state ls;
	parser_state *ps = NULL;
		
	JIO Jio;
	struct loadF lf;


	fp = fopen(fname, "rb");
	if(fp == NULL){
		return 0;
	}
	
	lf.fp = fp;

	io_init(&Jio, getF, &lf);	
	io_prefill(&Jio);

		lex_init(&ls, &Jio);
	
		ps = parser_new(ps, &ls);

	    ps->ct = new_ctable(ps->ct);

		parser(ps);
	fclose(fp);

	return call_newJcall(ps->chuck);
}